<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\finance;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsExcelInterface;
use app\common\model\finance\PaymentRecord;
use app\common\lists\ListsSearchInterface;


/**
 * payment列表
 * Class PaymentRecordLists
 * @package app\adminapi\listsfinance
 */
class PaymentRecordLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExcelInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2025/03/31 13:49
     */
    public function setSearch(): array
    {
        return [
            '=' => [
                'p.payment_date',
                'p.currency',
                'p.amount',
                'p.service_fee',
                'p.deposit_amount',
                'p.account',
                'p.remarks',
                'p.files',
                'p.status',
                'p.approval_user_id',
                'p.approval_time',
                'p.approval_remarks',
                'p.group_id',
                'p.user_id',
                'p.card_id',
                'p.card_no',
                'p.card_no_last4',
                'p.card_bin',
                'p.card_type',
            ],
            'like' => ['g.company_name'],
        ];
    }


    /**
     * @notes 获取payment列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2025/03/31 13:49
     */
    public function lists(): array
    {
        try {
            // 获取统计数据
            $statisticsResult = PaymentRecord::alias('p')
                ->join('sc_user_group g', 'p.group_id = g.id', 'left')
                ->where($this->searchWhere)
                ->field([
                    'ROUND(IFNULL(SUM(p.deposit_amount), 0), 2) as total_deposit_amount',
                    'ROUND(IFNULL(SUM(p.serverUamount), 0), 2) as total_serverUamount',
                    'ROUND(IFNULL(SUM(p.service_feeamount), 0), 2) as total_service_feeamount'
                ])
                ->find();

            $statistics = $statisticsResult ? $statisticsResult->toArray() : [
                'total_deposit_amount' => 0,
                'total_serverUamount' => 0,
                'total_service_feeamount' => 0
            ];

            // 获取列表数据
            $list = PaymentRecord::alias('p')
                ->join('sc_user_group g', 'p.group_id = g.id', 'left')
                ->where($this->searchWhere)
                ->field([
                    'p.id',
                    'p.payment_date',
                    'p.currency',
                    'p.amount',
                    'p.service_fee',
                    'p.serverUchargerate',
                    'p.serverUamount',
                    'p.service_feeamount',
                    'p.deposit_amount',
                    'p.pay_way',
                    'p.account',
                    'p.remarks',
                    'p.files',
                    'p.status',
                    'p.group_id',
                    'p.user_id',
                    'p.approval_user_id',
                    'p.approval_time',
                    'p.approval_remarks',
                    'g.company_name'
                ])
                ->limit($this->limitOffset, $this->limitLength)
                ->order(['p.id' => 'desc'])
                ->select()
                ->toArray();

            // 返回包含统计数据的完整结果
            if (isset($_GET['export'])) {
                return $list;
            }
            return [
                'list' => $list,
                'statistics' => $statistics
            ];
        } catch (\Exception $e) {
            // 记录错误日志
            \think\facade\Log::error('PaymentRecordLists lists error: ' . $e->getMessage());
            return [
                'list' => [],
                'statistics' => [
                    'total_deposit_amount' => 0,
                    'total_serverUamount' => 0,
                    'total_service_feeamount' => 0
                ]
            ];
        }
    }


    /**
     * @notes 获取payment数量
     * @return int
     * @author likeadmin
     * @date 2025/03/31 13:49
     */
    public function count(): int
    {
        try {
            return PaymentRecord::alias('p')
                ->join('sc_user_group g', 'p.group_id = g.id', 'left')
                ->where($this->searchWhere)
                ->count();
        } catch (\Exception $e) {
            // 记录错误日志
            \think\facade\Log::error('PaymentRecordLists count error: ' . $e->getMessage());
            return 0;
        }
    }

    /**
     * @notes 导出文件名
     */
    public function setFileName(): string
    {
        return '存款记录';
    }

    /**
     * @notes 导出字段
     */
    public function setExcelFields(): array
    {
        return [
            'id' => '交易id',
            'company_name' => '商户名称',
            'payment_date' => '打款日期',
            'currency' => '打款币种',
            'amount' => '打款金额',
            'service_fee' => '服务费率',
            'serverUchargerate' => '扫码服务费率',
            'serverUamount' => '扫码服务费',
            'service_feeamount' => '服务费金额',
            'deposit_amount' => '实收金额',
            'pay_way' => '打款方式',
            'remarks' => '备注',
            'status' => '状态(0:待处理,1:已确认,2:已拒绝,3:处理中,4:已完成)',
            'approval_user_id' => '审批人ID',
            'approval_time' => '审批时间',
            'approval_remarks' => '审批备注',

        ];
    }

}